package qs.crm.modules.client.cstCustomer.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import qs.crm.annotation.Log;
import qs.crm.modules.client.cstCustomer.domain.CstCustomer;
import qs.crm.modules.client.cstCustomer.service.CstCustomerService;
import qs.crm.modules.client.cstCustomer.service.dto.CstCustomerQueryCriteria;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author tong
* @date 2023-05-31
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "客户信息管理管理")
@RequestMapping("/api/cstCustomer")
public class CstCustomerController {

    private final CstCustomerService cstCustomerService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('cstCustomer:list')")
    public void exportCstCustomer(HttpServletResponse response, CstCustomerQueryCriteria criteria) throws IOException {
        cstCustomerService.download(cstCustomerService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询客户信息管理")
    @ApiOperation("查询客户信息管理")
    @PreAuthorize("@el.check('cstCustomer:list')")
    public ResponseEntity<Object> queryCstCustomer(CstCustomerQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(cstCustomerService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("查询客户总消费额")
    @ApiOperation("查询客户总消费额")
    @GetMapping(value = "/gongxian")
    @PreAuthorize("@el.check('cstCustomer:list')")
    public ResponseEntity<Object> gongxian(){
        Map<String,List<Map<String, BigDecimal>>> mlist = new HashMap<String,List<Map<String, BigDecimal>>>(){{
            put("content",cstCustomerService.gongxian());
        }};
        return new ResponseEntity<>(mlist,HttpStatus.OK);
    }

    @Log("查询客户构成")
    @ApiOperation("查询客户构成")
    @GetMapping(value = "/gongchen")
    @PreAuthorize("@el.check('cstCustomer:list')")
    public ResponseEntity<Object> gongchen(){
        Map<String,List<Map<String, Object>>> mlist = new HashMap<String,List<Map<String, Object>>>(){{
            put("content",cstCustomerService.gongchen());
        }};
        return new ResponseEntity<>(mlist,HttpStatus.OK);
    }

    @Log("查询客户服务分析")
    @ApiOperation("查询客户服务分析")
    @GetMapping(value = "/fenxi")
    @PreAuthorize("@el.check('cstCustomer:list')")
    public ResponseEntity<Object> fenxi(){
        Map<String,List<Map<String, Object>>> mlist = new HashMap<String,List<Map<String, Object>>>(){{
            put("content",cstCustomerService.fenxi());
        }};
        return new ResponseEntity<>(mlist,HttpStatus.OK);
    }


    @Log("查询客户流失分析")
    @ApiOperation("查询客户流失分析")
    @GetMapping(value = "/liushifenxi")
    @PreAuthorize("@el.check('cstCustomer:list')")
    public ResponseEntity<Object> liushifenxi(){
        Map<String,List<Map<String, Object>>> mlist = new HashMap<String,List<Map<String, Object>>>(){{
            put("content",cstCustomerService.liushifenxi());
        }};
        return new ResponseEntity<>(mlist,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增客户信息管理")
    @ApiOperation("新增客户信息管理")
    @PreAuthorize("@el.check('cstCustomer:add')")
    public ResponseEntity<Object> createCstCustomer(@Validated @RequestBody CstCustomer resources){
        return new ResponseEntity<>(cstCustomerService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改客户信息管理")
    @ApiOperation("修改客户信息管理")
    @PreAuthorize("@el.check('cstCustomer:edit')")
    public ResponseEntity<Object> updateCstCustomer(@Validated @RequestBody CstCustomer resources){
        cstCustomerService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除客户信息管理")
    @ApiOperation("删除客户信息管理")
    @PreAuthorize("@el.check('cstCustomer:del')")
    public ResponseEntity<Object> deleteCstCustomer(@RequestBody Long[] ids) {
        cstCustomerService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}